package com.mng.index12306.framework.starter.bases.init;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Description : 应用初始化后置处理器
 * @Author : YuShaochao
 * @Date : 2024-04-24 21:14
*/
@RequiredArgsConstructor
public class ApplicationContentPostProcessor implements ApplicationListener<ApplicationReadyEvent> {

    private final ApplicationContext applicationContext;

    private final AtomicBoolean executeOnlyOnce = new AtomicBoolean(false);


    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        if(!executeOnlyOnce.compareAndSet(false,true)){
            return;
        }
        applicationContext.publishEvent(new ApplicationInitializingEvent(this));
    }
}
